package com.u3k.app.mid;

import android.content.Context;
import com.u3k.app.mid.core.ModuleManager;
import com.u3k.app.mid.utils.common.Util;
import com.u3k.app.mid.utils.statistics.StatisticsManager;
import com.u3k.mid.*;
import com.u3k.mid.LogUtils;

import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONObject;

public class RegisterManager {
    public static String TAG = RegisterManager.class.getSimpleName();
    private static RegisterManager mInstance;
    public Context mContext;
    public boolean mRegistering;

    public RegisterManager(Context context) {
        this.mContext = context.getApplicationContext();
    }

    private String getCoreVersion() {
        JSONArray jSONArray = new JSONArray();
        Iterator it = ModuleManager.getInstance(this.mContext).getModuleInfos().iterator();
        while (it.hasNext()) {
            ModuleInfo uVar = (ModuleInfo) it.next();
            JSONObject jSONObject = new JSONObject();
            try {
                jSONObject.put("mName", uVar.name);
                jSONObject.put("coreVer", uVar.libVersion);
                jSONArray.put(jSONObject);
            } catch (Exception e) {
                LogUtils.a("error", e);
                StatisticsManager.getInstance().logException(ae.a(), e);
            }
        }
        return jSONArray.toString();
    }

    public static RegisterManager getInstance(Context context) {
        synchronized (RegisterManager.class) {
            try {
                if (mInstance == null) {
                    mInstance = new RegisterManager(context);
                }
                RegisterManager registerManager = mInstance;
                return registerManager;
            } finally {
                Object obj = RegisterManager.class;
            }
        }
    }

    private String getJsonParams(Context context) {
        JSONObject jSONObject = new JSONObject();
        try {
            jSONObject.put("phoneInfo", new aw(context, false).a());
            jSONObject.put("channelId", Util.getChannelId(context));
            jSONObject.put("appId", Util.getAppId(context));
            jSONObject.put("bundleId", Util.getBundleId(context));
            jSONObject.put("shellVer", Util.getShellVersion(context));
            jSONObject.put("middleVer", Util.getMiddleVersion(context));
            jSONObject.put("coreVer", getCoreVersion());
            jSONObject.put("usesPerm", Util.getUsedPermissionJson(context));
            jSONObject.put("selfPkgName", Util.getSelfPkgName(context));
            jSONObject.put("installPath", Util.getInstallDir(context));
            jSONObject.put("token", Util.getUserToken(context));
            jSONObject.put("srvImei", Util.getServImei(context));
            return jSONObject.toString();
        } catch (Exception e) {
            LogUtils.a("error", e);
            StatisticsManager.getInstance().logException(ae.a(), e);
            return "";
        }
    }

    public void register(Context context, RegisterListener registerListener) {
        LogUtils.LogError(TAG, "register");
        if (!Util.checkRegistered(context)) {
            if (this.mRegistering) {
                LogUtils.LogError(TAG, "register   mRegistering=true  return");
            } else {
                new m(this, context, registerListener).start();
            }
        }
    }
}
